# -*- python -*-
# ex: set syntax=python:
# Copyright 2014 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

# READ THIS:
# See http://dev.chromium.org/developers/testing/chromium-build-infrastructure

import os
import socket

# These modules come from scripts, which must be in the PYTHONPATH.
from master import master_utils
from master import slaves_list
from master.builders_pools import BuildersPools
from master.factory import annotator_factory
from master.try_job_rietveld import TryJobRietveld

import config
import master_site_config

ActiveMaster = master_site_config.BlinkTryServer


MAIL_NOTIFIER = ActiveMaster.is_production_host
UPDATE_CODEREVIEW = ActiveMaster.is_production_host
LISTEN_TO_RIETVELD = ActiveMaster.is_production_host

# This is the dictionary that the buildmaster pays attention to. We also use
# a shorter alias to save typing.
c = BuildmasterConfig = {}

config.DatabaseSetup(c, require_dbconfig=ActiveMaster.is_production_host)

c['change_source'] = []

# Avoid merging requests.
c['mergeRequests'] = lambda *_: False

m_annotator = annotator_factory.AnnotatorFactory(ActiveMaster)

c['builders'] = []
for platform in ['linux', 'mac', 'win']:
  for target in ['_dbg', '_compile_dbg', '_compile_rel', '_rel']:
    c['builders'].append({
      'name': '%s_blink%s' % (platform, target),
      'factory': m_annotator.BaseFactory('blink_trybot', timeout=3600),
      'slavebuilddir': '%s_layout' % platform
    })

for target in ['_dbg', '_rel']:
  c['builders'].append({
    'name': 'android_blink_compile%s' % target,
    'factory': m_annotator.BaseFactory('blink_android_trybot'),
    'slavebuilddir': 'android_layout'
  })

for platform in ['linux', 'mac', 'win']:
  for target in ['_dbg', '_rel']:
    c['builders'].append({
      'name': '%s_blink_oilpan%s' % (platform, target),
      'factory': m_annotator.BaseFactory('blink_trybot', timeout=3600),
      'slavebuilddir': '%s_layout' % platform
    })

c['builders'].append({
    'name': 'linux_chromium_gn_rel',
    'factory': m_annotator.BaseFactory('chromium_gn'),
    'slavebuilddir': 'linux_chromium_gn'
  })

c['builders'].append({
    'name': 'linux_blink_rel_ng',
    'factory': m_annotator.BaseFactory('chromium_trybot'),
    'slavebuilddir': 'linux_layout'
  })

def presubmit(name, slavebuilddir=None):
  return {
    'name': '%s_presubmit' % name,
    'slavebuilddir': (slavebuilddir or ('presubmit_%s' % name)),
    'factory': m_annotator.BaseFactory('run_presubmit', {'repo_name': name}),
  }

c['builders'].append({
  'name': 'blink_presubmit',
  'slavebuilddir': 'presubmit_blink',
  'factory': m_annotator.BaseFactory('run_presubmit', {'repo_name': 'blink'}),
})

slaves = slaves_list.SlavesList('slaves.cfg', 'BlinkTryServer')

for builder in c['builders']:
  builder['slavenames'] = slaves.GetSlavesName(builder=builder['name'])
  builder.setdefault('auto_reboot', ActiveMaster.is_production_host)

c['slaves'] = master_utils.AutoSetupSlaves(c['builders'],
                                           config.Master.GetBotPassword())

master_utils.VerifySetup(c, slaves)

c['schedulers'] = []

pools = BuildersPools('blink')
pools['blink'].extend([builder['name'] for builder in c['builders']])

code_review_sites = {'blink': ActiveMaster.code_review_site}

if LISTEN_TO_RIETVELD:
  c['schedulers'].append(TryJobRietveld(
        name='try_job_rietveld',
        pools=pools,
        code_review_sites=code_review_sites,
        project='blink',
        filter_master=True))

# Buildbot master url:
# Must come before AutoSetupMaster().
if ActiveMaster.is_production_host:
  c['buildbotURL'] = ActiveMaster.buildbot_url
else:
  c['buildbotURL'] = 'http://%s:%d/' % (
      socket.getfqdn(), ActiveMaster.master_port)

# Adds common status and tools to this master.
# Use our own mail notifier.
master_utils.AutoSetupMaster(c, ActiveMaster, False)

if MAIL_NOTIFIER:
  # Add a dumb MailNotifier first so it will be used for BuildSlave with
  # notify_on_missing set when they go missing.
  from buildbot.status import mail
  c['status'].append(mail.MailNotifier(
      fromaddr=ActiveMaster.from_address,
      builders=[],
      relayhost=config.Master.smtp,
      lookup=master_utils.UsersAreEmails()))

  from master import chromium_notifier
  c['status'].append(chromium_notifier.ChromiumNotifier(
      fromaddr=ActiveMaster.from_address,
      categories_steps={'': ['update_scripts']},
      relayhost=config.Master.smtp,
      status_header=
          '%(steps)s failed on slave "%(slavename)s" for builder "%(builder)s"',
      subject='%(steps)s failed on trybot %(slavename)s on %(date)s',
      sendToInterestedUsers=False,
      extraRecipients=['chrome-troopers+tryalert@google.com'],
      use_getname=True,
      enable_mail=ActiveMaster.is_production_host))

  # Try job result emails.
  from master.try_mail_notifier import TryMailNotifier
  c['status'].append(TryMailNotifier(
      fromaddr=ActiveMaster.from_address,
      reply_to=ActiveMaster.reply_to,
      subject="try %(result)s for %(reason)s @ r%(revision)s",
      mode='all',
      relayhost=config.Master.smtp,
      lookup=master_utils.UsersAreEmails()))

if UPDATE_CODEREVIEW:
  from master.status_push import TryServerHttpStatusPush
  c['status'].append(
      TryServerHttpStatusPush(serverUrl=ActiveMaster.code_review_site))


# The followings are what is kept on disk.
# Keep last try jobs, the default is too low. Must keep at least a few days
# worth of try jobs. 3000 is not even a full day but the server is full. Keep
# more build objects than log since they are much smaller.
c['buildHorizon'] = 6000
c['logHorizon'] = 3000
# Must be at least 2x the number of slaves.
c['eventHorizon'] = 200
c['logCompressionLimit'] = False

# Adjust the buildCaches to be 3x the number of slaves per builder.
c['autoBuildCacheRatio'] = 3

c['projectURL'] = 'http://dev.chromium.org/developers/testing/try-server-usage'

# vi: set ts=4 sts=2 sw=2 et:
